package com.wiiss.ams.ding;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class TextMsgRobot {
    private static final Logger log = LoggerFactory.getLogger(TextMsgRobot.class);

    public static void sendRobotMsgByTest (String webhookToken, boolean asAll, String content, List<String> mobiles, List<String> userIds) {
        try {
            // 机器人webhook对应的access_token的值，不是client_id和client_secret生成的应用access_token
            // 创建钉钉客户端
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send");
            // 构建请求对象
            OapiRobotSendRequest req = new OapiRobotSendRequest();
            req.setMsgtype("text"); // 消息类型为text
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(content); // 文本内容
            req.setText(text);
            // 构建@信息
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setIsAtAll(asAll); // 是否@所有人
            // 指定@的用户ID列表
            at.setAtUserIds(userIds);
            at.setAtMobiles(mobiles);
            req.setAt(at);
            // 发送请求
            OapiRobotSendResponse rsp = client.execute(req, webhookToken);
            if (rsp.getErrcode() != 0) {
                log.info("调用送鼎+接口发送失败");
            }
        } catch (ApiException e) {
            log.error("发送鼎+失败", e);
        }
    }

    public static void main(String[] args) {
        TextMsgRobot.sendRobotMsgByTest("2275cf8284b22803a21950648dd054dae81d118889ddf59067233c5f970b5f3c", false, "设备。。。。", new ArrayList<>(), new ArrayList<>());
    }

}